package com.steven.ourcontest.chat.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.chat.api.ChatApi
import com.steven.ourcontest.chat.bean.ChatOnLineRes
import com.steven.ourcontest.utils.plusAssign

class ChatViewModel : BaseViewModel() {
    private val _queryMessagesResponse = MutableLiveData<ChatOnLineRes>()
    val queryMessagesResponse: LiveData<ChatOnLineRes>
        get() = _queryMessagesResponse

    private val _sendMessagesResponse = MutableLiveData<Boolean>()
    val sendMessagesResponse: LiveData<Boolean>
        get() = _sendMessagesResponse

    var lastCreateTime = 0L
    fun queryMessage(friendId:Long,successCallBack:(()->Unit)?=null) {
        disposed += ChatApi.instance.query2UserAllMessage(friendId,lastCreateTime)
            .subscribe({
                _queryMessagesResponse.value = it
                successCallBack?.invoke()
                lastCreateTime = it.data.last().createTime
            }, {
                it.printStackTrace()
            })
    }

    fun sendMessage(friendId:Long,message:String){
        disposed += ChatApi.instance.sendMessageTo(friendId,message)
            .subscribe({
                _sendMessagesResponse.value = true
            }, {
                _sendMessagesResponse.value = false
                it.printStackTrace()
            })
    }

    fun updateMessageStatus(sendUser:Long) {
        disposed += ChatApi.instance.updateMessageStatus(sendUser)
            .subscribe({
            }, {
                it.printStackTrace()
            })
    }
}